import { defineStore } from 'pinia'
import { IArticleItem, IResponse } from '../types'

import request from '../utils/request'

//创建channels模块并导出
export default defineStore('news', {
  state() {
    return {
      newsList: [] as IArticleItem[], //装频道的新闻数据
    }
  },
  actions: {
    //根据频道ID获取文章列表的action
    async getArticleByChannelId(id: number) {
      //定义IArticleItem类型
      const r = await request.get<
        IResponse<{ pre_timestamp: string; results: IArticleItem[] }>
      >(`/articles?channel_id=${id}&timestamp=${Date.now()}`)
      console.log(r)
      //存储到newslist中
      this.newsList = r.data.data.results
    },
  },
})
